package cn.hl.hm.logic.st;

import cn.hl.hm.constants.DriverWorkStatus;
import cn.hl.hm.constants.OrderStatus;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverVehicle;
import cn.hl.hm.entity.Order;
import cn.hl.hm.entity.Route;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.DriverVehicleService;
import cn.hl.hm.service.OrderService;
import cn.hl.hm.service.RouteService;
import cn.hl.hm.util.OrderUtil;
import cn.hl.hm.vo.StOrderVo;
import com.vizhuo.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by xianghan on 17/12/13.
 */
@Service
public class StOrderLogic {
    private static final Logger logger = LoggerFactory.getLogger(StOrderLogic.class);
    @Autowired
    private OrderService orderService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private DriverVehicleService driverVehicleService;
    @Autowired
    private DriverService driverService;
//    @Autowired
//    private StDriverLogic stDriverLogic;

    public boolean syncStOrderToHm(StOrderVo stOrderVo) {
        try {
            if (StringUtils.isNotBlank(stOrderVo.getPackCode())) {
                Order order = new Order();
                order.setStOrderId(Integer.valueOf(stOrderVo.getPackCode()));
                List<Order> orderList = orderService.selectByPrimaryKeySelective(order);

                order.setSenderName(stOrderVo.getSendUserName());
                order.setSenderPhone(stOrderVo.getSendUserPhone());
                order.setSenderCompanyName(stOrderVo.getBusinessName());
                order.setReciveverPhone(stOrderVo.getReceiverPhone());
                order.setUseTime(DateUtil.string2Date(stOrderVo.getPackageTime()));
                order.setGoodsName(stOrderVo.getGoodsName());
                order.setGoodsNumber(stOrderVo.getGoodsNumber());
                order.setTotalPrice(stOrderVo.getTotalPrice());
                order.setPayPrice(order.getTotalPrice());
                order.setOrderStatus(stOrderVo.getStatus());
                order.setUserAccount(stOrderVo.getSendUserPhone());
                order.setStLineId(StringUtils.isNotBlank(stOrderVo.getStLineId()) ? Integer.valueOf(stOrderVo.getStLineId()) : null);
                order.setGoodsName("百货");
                //TODO 需要和大和确认
//                order.setPayTime();
//                order.setUserEnsureTime();
//                order.setOperId();
//                order.setPayType();
                Integer lineId = StringUtils.isNotBlank(stOrderVo.getHmLineId()) ? Integer.valueOf(stOrderVo.getHmLineId()) : null;
                if (lineId != null) {
                    Route route = new Route();
                    route.setId(lineId);
                    List<Route> routes = routeService.selectRoute(route);
                    if (routes != null && routes.size() > 0) {
                        order.setLineId(lineId);
                        order.setLineName(routes.get(0).getStartPoint() + "-" + routes.get(0).getEndPoint());
                        order.setSrcAddress(routes.get(0).getStartPoint());
                        order.setDestAddress(routes.get(0).getEndPoint());
                        order.setTotalMileage(routes.get(0).getRouteMileage() == null ? null : BigDecimal.valueOf(routes.get(0).getRouteMileage()));
                    }
                }
                DriverVehicle driverVehicle=new DriverVehicle();
                Driver driver=null;
                Integer driverVehicleId = StringUtils.isNotBlank(stOrderVo.getDriverVehicleId()) ? Integer.valueOf(stOrderVo.getDriverVehicleId()) : null;
                if (driverVehicleId != null) {
                    driverVehicle = driverVehicleService.selectByPrimaryKey(driverVehicleId);
                    order.setCarId(driverVehicle.getVehicleTypeId());
                    order.setDriverId(driverVehicle.getDriverId());
                    driver = driverService.selectByPrimaryKey(driverVehicle.getDriverId());
                    if (driver != null) {
                        order.setDriverName(driver.getName());
                        order.setDriverPhone(driver.getDriverPhone());
                    }
                }
                //更新
                if(orderList!=null && orderList.size()>0){
                    order.setId(orderList.get(0).getId());
                    orderService.updateOrderById(order);

                }else{//新增
                    order.setOrderCode(OrderUtil.getInstance().getOrderCode());
                    order.setCreateTime(new Date());
                    orderService.add(order);
                }
                if (driverVehicle!=null) {
                    if (stOrderVo.getStatus().equals(OrderStatus.TO_ACCEPT.getStatus())
                            || stOrderVo.getStatus().equals(OrderStatus.TO_ARRIVE.getStatus())){
                        driverVehicle.setWorkStatus(Byte.valueOf(DriverWorkStatus.BUSY.getStatus()));
                        driver.setWorkStatus(DriverWorkStatus.BUSY.getStatus());
                    }else if(stOrderVo.getStatus().equals(OrderStatus.TO_COMMENT.getStatus())){
                        driverVehicle.setWorkStatus(Byte.valueOf(DriverWorkStatus.FREE.getStatus()));
                        driver.setWorkStatus(DriverWorkStatus.FREE.getStatus());
                    }
                    driverService.updateByPrimaryKeySelective(driver);
                    driverVehicleService.updateByPrimaryKeySelective(driverVehicle);

                    //是否立马通知干线修改司机和车辆使用状态
//                    stDriverLogic.ayncVehicel(driverVehicle.getId());
                }
                return true;

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;


    }

}
